package com.zone.core.helper

import com.liulishuo.okdownload.DownloadListener
import com.liulishuo.okdownload.DownloadTask
import com.zone.core.CoreApplication
import java.io.File

object DownloadHelper {
    val context = CoreApplication.coreApp

    //应用缓存路径
    private val fileCache = context.cacheDir


    fun startDownload(
        url: String,
        file: File = fileCache,
        threads: Int = 1,
        callbackProcess: Int = 500,
        priority: Int = 0,
        listener: DownloadListener? = null
    ): DownloadTask {
        val task = DownloadTask.Builder(url, file)
            .setConnectionCount(threads)  //需要用几个线程来下载文件
            .setFilenameFromResponse(true)
            //.setHeaderMapFields(new HashMap<String, List<String>>())//设置请求头
            //.addHeader(String key, String value)//追加请求头
            .setPriority(priority)//设置优先级，默认值是0，值越大下载优先级越高
            .setMinIntervalMillisCallbackProcess(callbackProcess)//下载进度回调的间隔时间（毫秒)
            .build()
        task.enqueue(listener)
        return task
    }


}